<?php

namespace App\Http\Controllers\Home;


use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Models\Cate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class CateController extends Controller
{
    const DATA_NUMBER=6;//数据条数

    /**
     * 进入分页后的数据
     */
    public function index(Request $request)
    {
        $cate=Cate::find($request->cateId);
        $cateArr=$cate->toArray();
        $data['cateChild']=array_merge([$cateArr],$cate->children->toArray());

        return ['status_code'=>200,'message'=>'数据获取成功','result'=>$data];
    }

    /**
     *获取分类下的所有文章
     */
    public function articleByCate(Request $request)
    {
        $cateId=$request->cateId;

        $childrenIds=Cate::where('parent_id',$cateId)->pluck('id')->toArray();
        $childrenIds[]=$cateId;


        $data=Article::with('cate:id,name')
            ->whereIn('cate_id',$childrenIds)
            ->select('id','title','thumb','created_at','author','description','cate_id')
            ->orderBy('id','desc')
            ->paginate(self::DATA_NUMBER);
        return ['status_code'=>200,'message'=>'数据获取成功','result'=>$data];
    }

}
